﻿using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Windows.Media;

namespace MouseExtender.Logic.Entities
{
    [DataContract]
    public class LauncherItem : INotifyPropertyChanged
    {
        private string _iconSource = null;
        private string _thumbsId = null;

        [DataMember]
        public string LaunchPath
        {
            get;
            set;
        }

        [DataMember(IsRequired = false)]
        public string Arguments
        {
            get;
            set;
        }

        [DataMember(IsRequired = false)]
        public string WorkingDirectory
        {
            get;
            set;
        }

        [DataMember(IsRequired = false)]
        public string IconSource
        {
            get
            {
                if (String.IsNullOrEmpty(_iconSource))
                {
                    _iconSource = LaunchPath;
                }
                return _iconSource;
            }
            set
            {
                _iconSource = value;
            }
        }

        [DataMember(IsRequired = false)]
        public int IconIndex
        {
            get;
            set;
        }

        [DataMember]
        public int ItemNumber
        {
            get;
            set;
        }

        [DataMember]
        public Color ItemColor
        {
            get;
            set;
        }

        [DataMember]
        public string ToolTip
        {
            get;
            set;
        }

        [DataMember]
        public int TabId
        {
            get;
            set;
        }

        [DataMember]
        public bool IsUrl
        {
            get;
            set;
        }

        [DataMember]
        public string ThumbsImageId
        {
            get
            {
                return _thumbsId;
            }
            set
            {
                if (_thumbsId != value)
                {
                    _thumbsId = value;

                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("ThumbsImageId"));
                }
            }
        }

        /// <summary>
        /// Use by cache dictionary. If item has been updated that hash changed and it won;t be found at launcher item hash.
        /// </summary>
        /// <returns>object hash</returns>
        public override int GetHashCode()
        {
            return base.GetHashCode() //this require in order to differ different object with the same fields because object address included into hash calculation 
                ^ (this.Arguments == null ? 0 : this.Arguments.GetHashCode())
                ^ this.IconIndex.GetHashCode()
                ^ (this.IconSource == null ? 0 : this.IconSource.GetHashCode())
                ^ this.IsUrl.GetHashCode()
                ^ this.ItemColor.GetHashCode()
                ^ this.ItemNumber.GetHashCode()
                ^ (this.LaunchPath == null ? 0 : this.LaunchPath.GetHashCode())
                ^ this.TabId.GetHashCode()
                ^ (this.ThumbsImageId == null ? 0 : this.ThumbsImageId.GetHashCode())
                ^ (this.ToolTip == null ? 0 : this.ToolTip.GetHashCode())
                ^ (this.WorkingDirectory == null ? 0 : this.WorkingDirectory.GetHashCode());
        }


        /// <summary>
        /// Clones LauncherItem object
        /// </summary>
        /// <returns>Clone of current LauncherItem object</returns>
        public LauncherItem Clone()
        {
            LauncherItem item = new LauncherItem()
            {
                Arguments = this.Arguments,
                IconIndex = this.IconIndex,
                IconSource = this.IconSource,
                ItemColor = this.ItemColor,
                ItemNumber = this.ItemNumber,
                TabId = this.TabId,
                ToolTip = this.ToolTip,
                WorkingDirectory = this.WorkingDirectory,
                LaunchPath = this.LaunchPath,
                IsUrl = this.IsUrl,
                ThumbsImageId = this.ThumbsImageId
            };
            return item;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
